<?php
/*
authored by Josh Fraser (www.joshfraser.com)
released under Apache License 2.0

Maintained by Alexander Makarov, http://rmcreative.ru/

$Id$
*/
error_reporting(E_ERROR|E_WARNING);
require_once("RollingCurl.php");
require_once("Util.php");
require_once("DbHelper.php");

// a little example that fetches a bunch of sites in parallel and echos the page title and response info for each request
function request_callback($response, $info, $request) {
	// parse the page title out of the returned HTML
	//<h3 class="f18 l26"><a.*href="(.*\.htm)".*>.*</a>.*</h3>
	if (preg_match_all('~<h3 class="f18 l26"><a.*href="(.*\.htm)".*>.*</a>.*</h3>~i', $response, $out)) {
		$listlinks= $out[1];
		getcontent($listlinks);
	}
	else{
		return;
	}
}
// single curl request
$rc = new RollingCurl("request_callback");
$urls=array("http://digi.tech.qq.com/c/gdywlist_1.htm",
		"http://digi.tech.qq.com/c/gdywlist_2.htm",
		"http://digi.tech.qq.com/c/gdywlist_2.htm"
	);
$rc->window_size = 20;
foreach ($urls as $url) {
    $request = new RollingCurlRequest($url);
    $rc->add($request);
}
$rc->execute();



function getcontent($urls)
{
	$rc = new RollingCurl("content_callback");
	$rc->window_size = 100;
	foreach ($urls as $url) {
		if (DbHelper::IsFetchUrl($url)) {
			$request = new RollingCurlRequest($url);
			$rc->add($request);
		}
	}
	$rc->execute();
}


function content_callback($response, $info, $request)
{
	$regs= array('title'=>'<h1>(.*)</h1>',
				 'createtime'=>'<span class="pubTime">(.*)</span>',
				 'content'=>'<div id="Cnt-Main-Article-QQ" bossZone="content">(<p [style|align]?.*>.*</p>)'
				);	
	$flag= true;
	$data=array();
	foreach($regs as $key=>$value)
	{
		
		if (preg_match('~'.$value.'~i', $response, $out)) {
			//iconv("GB2312","UTF-8"."//IGNORE",$html);
			$content = $out[1];
			$content=iconv("GB2312","UTF-8"."//IGNORE",$content);
			if($key=='createtime'){
				$content =str_replace('年','-',$content);
				$content =str_replace('月','-',$content);
				$content =str_replace('日',' ',$content);
				$content =strtotime($content);
				//echo  $content;
				$today = strtotime(date("Y-m-d"));
				if($content<$today)
				{
					$flag=false;
					return;
				}
			}
			if($key=='content')
			{
				$imgs= Util::getimgs($content);
				foreach($imgs as $img){
					$content=str_replace($img['oldimg'],$img['newimg'],$content);
				}
			}
			$data[$key]= $content;
		}
		else
		{
		    $flag=false;
			return;
		}
	}
	if($flag)
	{
		DbHelper::save($data,$request->url);
	}
}













// top 20 sites according to alexa (11/5/09)
/*
$urls = array("http://www.google.com",
              "http://www.facebook.com",
              "http://www.yahoo.com",
              "http://www.youtube.com",
              "http://www.live.com",
              "http://www.wikipedia.com",
              "http://www.blogger.com",
              "http://www.msn.com",
              "http://www.baidu.com",
              "http://www.yahoo.co.jp",
              "http://www.myspace.com",
              "http://www.qq.com",
              "http://www.google.co.in",
              "http://www.twitter.com",
              "http://www.google.de",
              "http://www.microsoft.com",
              "http://www.google.cn",
              "http://www.sina.com.cn",
              "http://www.wordpress.com",
              "http://www.google.co.uk");

$rc = new RollingCurl("request_callback");
$rc->window_size = 20;
foreach ($urls as $url) {
    $request = new RollingCurlRequest($url);
    $rc->add($request);
}
$rc->execute();
*/
